<?php
/**
 *
 * @project ab
 * @filename AbInput.php
 * @author kb mk9007@163.com
 * @createtime  2022-04-2022/4/18 16:17
 */

class AbInput
{
    protected $requests = 10;

    protected $concurrency = 10;

    /**
     * 包含了需要POST的数据的文件.
     * @var string
     */
    protected $postfile = '';

    /**
     * 需要提交的数据
     * @var
     */
    public $postData = [
        'environment' => 'ab-test'
    ];

    /**
     * 提交方式
     * @var string
     */
    public $method = 'GET';

    /**
     *  cookie-name=value 对请求附加一个Cookie:行。 其典型形式是name=value的一个参数对。此参数可以重复。
     * @var array
     */
    protected $cookie = [
        'environment' => 'ab-test'
    ];

    /**
     * 测试所进行的最大秒数。其内部隐含值是-n 50000。它可以使对服务器的测试限制在一个固定的总时间以内。默认时，没有时间限制。
     * @var int
     */
    protected $timelimit = '';


    /**
     * 即content-type，用于设置Content-Type请求头信息，例如：application/x-www-form-urlencoded，默认值为text/plain。
     * @var string
     */
    protected $contentType = 'application/x-www-form-urlencoded';


    protected $url = '';

    /**
     * @return mixed
     */
    public function url()
    {
        $queryString = '';
        //如果是GET请求，把需要写单的参数进行补充
        if ($this->method == 'GET' && $this->postData) {
            $queryString = (stripos($this->url, '?') === false ? '?' : '') . http_build_query($this->postData);
        }
        return $this->url . $queryString;
    }

    /**
     * @param mixed $url
     */
    public function setUrl($url)
    {
        $this->url = $url;
    }


    /**
     * //在测试会话中所执行的请求个数。默认时，仅执行一个请求
     * @return int
     */
    public function getRequests()
    {
        return $this->requests ? "-n {$this->requests}" : "";
    }

    /**
     * @return int
     */
    public function getTimelimit()
    {
        return $this->timelimit ? "-t {$this->requests}" : "";
    }

    /**
     * @param int $timelimit
     */
    public function setTimelimit($timelimit)
    {
        $this->timelimit = $timelimit;
    }


    /**
     * @param int $requests
     */
    public function setRequests($requests)
    {
        $this->requests = $requests;
    }

    /**
     * @return int
     */
    public function getConcurrency()
    {
        return $this->concurrency ? "-c {$this->concurrency}" : "";
    }

    /**
     * @param int $concurrency
     */
    public function setConcurrency($concurrency)
    {
        $this->concurrency = $concurrency;
    }


    public function buildVal()
    {
        if ($this->method == 'POST' && $this->postData) {
            if (!is_dir('var')) {
                mkdir('var', 0777, true);
                chmod('var', 0777);
            }
            $val = http_build_query($this->postData);
            file_put_contents('var' . DIRECTORY_SEPARATOR . md5($val), $val);
            $this->postfile = 'var' . DIRECTORY_SEPARATOR . md5($val);
        }
        $methods = get_class_methods($this);
        $str = "";
        foreach ($methods as $item) {
            if (stripos($item, 'get') !== false) {
                $str .= (" " . $this->$item());
            }
        }
        return $str;
    }

    /**
     * 解析获取是使用https还是http
     * @return string
     * @author kb mk9007@163.com
     *2022-04-2022/4/18  16:33
     */
    public function abExe()
    {
        return (parse_url($this->url())['scheme'] ?? '') === 'https' ? 'abs.exe' : 'ab.exe';
    }

    /**
     * @return string
     */
    public function getPostfile()
    {
        return $this->postfile ? "-p {$this->postfile}" : "";;
    }

    /**
     * @param string $postfile
     */
    public function setPostfile($postfile)
    {
        $this->postfile = $postfile;
    }

    /**
     * @return string
     */
    public function getContentType()
    {
        return $this->contentType ? "-T {$this->contentType}" : "";
    }

    /**
     * @param string $contentType
     */
    public function setContentType($contentType)
    {
        $this->contentType = $contentType;
    }

    /**
     * @return array
     */
    public function getCookie()
    {
        return $this->cookie ? "-C " . http_build_query($this->cookie) : '';
    }

    /**
     * @param array $cookie
     */
    public function setCookie($cookie)
    {
        $this->cookie = $cookie;
    }

    public function finish()
    {
        if ($this->postfile) {
            @unlink($this->postfile);
            $this->postfile = '';
        }
    }

}